package org.adream.account.service.ws;

import java.net.URL;

import javax.annotation.Resource;
import javax.xml.namespace.QName;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

@Component
@EnableAsync
public class SynDataService {

	private final static Logger logger = LoggerFactory.getLogger(SynDataService.class);

	private static final QName SERVICE_NAME = new QName("http://soap.sforce.com/schemas/class/HTWWebServices",
			"CoreAction");

	@Resource
	private TaskExecutor taskExecutor;

	/**
	 * 同步盒子数据
	 * 
	 * @param data
	 * @return
	 */
	@Async
	public void synDreamData(SynDataEntity data) {
		logger.debug("同步盒子数据");
		URL wsdlURL = CoreAction.WSDL_LOCATION;
		CoreAction coreAction = new CoreAction(wsdlURL, SERVICE_NAME);
		CoreType port = coreAction.getCoreAction();
		// 添加认证header
		Client client = ClientProxy.getClient(port);
		String clientId = "account.adream.org";
		String clientToken = "uCdefe333$$$u";
		// String timestamp = String.valueOf(System.currentTimeMillis());
		String timestamp = "1111111111";
		// String clientSecret = "$22123Kdfffxn8";
		String clientSecret = "8428fd12d8fc166fe1bf5b253bb95322";
		// String clientSecret = PasswordUtil.md5Hex(clientId + clientToken + timestamp);
		client.getOutInterceptors().add(new AuthInterceptor(clientId, clientToken, timestamp, clientSecret));

		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				port.updateUserInfo(data.getUid(), data.getUname(), data.getSex(), data.getEmail(), data.getPhone(),
						data.getProvince(), data.getCity(), data.getArea(), data.getIntro(), data.getHeadimgurl(),
						data.getVerified(), data.getRealname(), data.getSchoolNumber(), data.getIsTeacher(),
						data.getIsDreamDirector(),data.getIsAdreamboxAdmin(),data.getIsVolunteer());
			}
		});
	}

	/**
	 * 同步盒子数据
	 * @param data
	 * @return
	 */
	public WebServiceResponse synData(SynDataEntity data) {
		logger.debug("同步盒子数据");
		URL wsdlURL = CoreAction.WSDL_LOCATION;
		CoreAction coreAction = new CoreAction(wsdlURL, SERVICE_NAME);
		CoreType port = coreAction.getCoreAction();
		// 添加认证header
		Client client = ClientProxy.getClient(port);
		String clientId = "account.adream.org";
		String clientToken = "uCdefe333$$$u";
		// String timestamp = String.valueOf(System.currentTimeMillis());
		String timestamp = "1111111111";
//		 String clientSecret = "$22123Kdfffxn8";
		String clientSecret = "8428fd12d8fc166fe1bf5b253bb95322";
		// String clientSecret = PasswordUtil.md5Hex(clientId + clientToken + timestamp);
		client.getOutInterceptors().add(new AuthInterceptor(clientId, clientToken, timestamp, clientSecret));

		WebServiceResponse result = port.updateUserInfo(data.getUid(), data.getUname(), data.getSex(), data.getEmail(), data.getPhone(),
				data.getProvince(), data.getCity(), data.getArea(), data.getIntro(), data.getHeadimgurl(),
				data.getVerified(), data.getRealname(), data.getSchoolNumber(), data.getIsTeacher(),
				data.getIsDreamDirector(),data.getIsAdreamboxAdmin(),data.getIsVolunteer());
		return result;
	}
}